#include "ColumnSet.h"
#include <iostream>

namespace duomai {
    namespace im {

        ColumnSet::ColumnSet (vector<Column>& columns) {
            m_Vector = columns;
            for (int index = 0; index < (int)columns.size(); index++) {
                Column &column = m_Vector[index];
                column.setIndex(index);
                m_Map[column.getColumnName()] = &column;
            }
        }

        ColumnSet::~ColumnSet () {
            //TODO
        }

        ColumnSet::ColumnSet (const ColumnSet& columns) {
            //TODO
        }

        Column* ColumnSet::getByIndex(int index) {

            if ((index >= 0) && (index < getCount())) {
                return &m_Vector[index];
            }

            return NULL;
        }
        Column* ColumnSet::getByName(string name) {
            return m_Map[name];
        }
        int ColumnSet::getCount() {
            return (int)m_Vector.size();
        }
        string ColumnSet::toString() {
            string s;
            for (int index = 0; index < getCount(); index++)
            {
                Column* pColumn = getByIndex(index);

                if (pColumn)
                {
                    s += pColumn->getColumnName();
                    if (index < (getCount() - 1))
                        s += ", ";
                } else {
                    std::cerr << "ColumnName at(%d)" << index << " is NULL" << std::endl;
                }
            }
            return s;
        }

        string ColumnSet::toSQLCreate() {
            string s;

            for (int index = 0; index < getCount(); index++)
            {
                if (Column* f = getByIndex(index))
                {
                    s += f->toSQLCreate();
                    if (index < (getCount() - 1))
                        s += ", ";
                }
            }

            return s;
        }
    } /* im */

} /* duomai  */
